﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using TestTool.GUI.Data;
    using TestTool.GUI.Views;
    using TestTool.Tests.Definitions.Trace;
    using System.Linq;

    public class TestResultsControl : UserControl, ITestResultView
    {
        private IContainer components;
        private ColumnHeader hdrDetails;
        private ColumnHeader hdrResult;
        private ColumnHeader hdrStep;
        private ImageList ilStepIcons;
        private Label lblRequest;
        private Label lblResponse;
        private ListView lvStepDetails;
        private SplitContainer scStepDetails;
        private SplitContainer scTraffic;
        private TextBox tbRequest;
        private TextBox tbResponse;
        private TextBox tbTestResult;
        private TabControl tcTestResults;
        private TabPage tpStepDetails;
        private TabPage tpTestResult;

        public TestResultsControl()
        {
            this.InitializeComponent();
        }

        public void AddStepResult(StepResult result)
        {
            ListViewItem item = new ListViewItem(result.Number.ToString())
            {
                Tag = result
            };
            item.SubItems.Add(result.Status.ToString());
            item.SubItems.Add(result.Message);
            item.ImageKey = (string.IsNullOrEmpty(result.Request) && string.IsNullOrEmpty(result.Response)) ? string.Empty : "NetworkOperation";
            this.lvStepDetails.Items.Add(item);
        }

        public void BeginTest()
        {
            base.BeginInvoke(new Action(() => this.ClearTestInfo()));
        }

        public void ClearTestInfo()
        {
            this.lvStepDetails.Items.Clear();
            this.tbResponse.Clear();
            this.tbRequest.Clear();
            this.tbTestResult.Clear();
        }

        public void DisplayProfileTestLog(string dump)
        {
            this.ClearTestInfo();
            this.tbTestResult.Text = dump;
        }

        public void DisplayStepResult(StepResult result)
        {
            base.BeginInvoke(new Action(() => this.AddStepResult(result)));
        }

        public void DisplayTestResults(TestResult log)
        {
            if (log.Log != null)
            {
                foreach (StepResult result in from s in log.Log.Steps
                                              orderby s.Number
                                              select s)
                {
                    this.AddStepResult(result);
                }
            }
            this.tbTestResult.Text = log.PlainTextLog;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void EnableControl(bool bEnable)
        {
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(TestResultsControl));
            this.tcTestResults = new TabControl();
            this.tpTestResult = new TabPage();
            this.tbTestResult = new TextBox();
            this.tpStepDetails = new TabPage();
            this.scStepDetails = new SplitContainer();
            this.lvStepDetails = new ListView();
            this.hdrStep = new ColumnHeader();
            this.hdrResult = new ColumnHeader();
            this.hdrDetails = new ColumnHeader();
            this.scTraffic = new SplitContainer();
            this.tbRequest = new TextBox();
            this.lblRequest = new Label();
            this.tbResponse = new TextBox();
            this.lblResponse = new Label();
            this.ilStepIcons = new ImageList(this.components);
            this.tcTestResults.SuspendLayout();
            this.tpTestResult.SuspendLayout();
            this.tpStepDetails.SuspendLayout();
            this.scStepDetails.Panel1.SuspendLayout();
            this.scStepDetails.Panel2.SuspendLayout();
            this.scStepDetails.SuspendLayout();
            this.scTraffic.Panel1.SuspendLayout();
            this.scTraffic.Panel2.SuspendLayout();
            this.scTraffic.SuspendLayout();
            base.SuspendLayout();
            this.tcTestResults.Controls.Add(this.tpTestResult);
            this.tcTestResults.Controls.Add(this.tpStepDetails);
            this.tcTestResults.Dock = DockStyle.Fill;
            this.tcTestResults.Location = new Point(0, 0);
            this.tcTestResults.Name = "tcTestResults";
            this.tcTestResults.SelectedIndex = 0;
            this.tcTestResults.Size = new Size(0x1da, 0x1a8);
            this.tcTestResults.TabIndex = 4;
            this.tpTestResult.Controls.Add(this.tbTestResult);
            this.tpTestResult.Location = new Point(4, 0x16);
            this.tpTestResult.Name = "tpTestResult";
            this.tpTestResult.Padding = new Padding(3);
            this.tpTestResult.Size = new Size(0x1d2, 0x18e);
            this.tpTestResult.TabIndex = 0;
            this.tpTestResult.Text = "Output";
            this.tpTestResult.UseVisualStyleBackColor = true;
            this.tbTestResult.Dock = DockStyle.Fill;
            this.tbTestResult.Location = new Point(3, 3);
            this.tbTestResult.Multiline = true;
            this.tbTestResult.Name = "tbTestResult";
            this.tbTestResult.ReadOnly = true;
            this.tbTestResult.ScrollBars = ScrollBars.Vertical;
            this.tbTestResult.Size = new Size(460, 0x188);
            this.tbTestResult.TabIndex = 4;
            this.tpStepDetails.Controls.Add(this.scStepDetails);
            this.tpStepDetails.Location = new Point(4, 0x16);
            this.tpStepDetails.Name = "tpStepDetails";
            this.tpStepDetails.Padding = new Padding(3);
            this.tpStepDetails.Size = new Size(0x1d2, 0x18e);
            this.tpStepDetails.TabIndex = 1;
            this.tpStepDetails.Text = "Step Details";
            this.tpStepDetails.UseVisualStyleBackColor = true;
            this.scStepDetails.Dock = DockStyle.Fill;
            this.scStepDetails.Location = new Point(3, 3);
            this.scStepDetails.Name = "scStepDetails";
            this.scStepDetails.Orientation = Orientation.Horizontal;
            this.scStepDetails.Panel1.Controls.Add(this.lvStepDetails);
            this.scStepDetails.Panel2.Controls.Add(this.scTraffic);
            this.scStepDetails.Size = new Size(460, 0x188);
            this.scStepDetails.SplitterDistance = 0x7f;
            this.scStepDetails.TabIndex = 6;
            this.lvStepDetails.Columns.AddRange(new ColumnHeader[] { this.hdrStep, this.hdrResult, this.hdrDetails });
            this.lvStepDetails.Dock = DockStyle.Fill;
            this.lvStepDetails.FullRowSelect = true;
            this.lvStepDetails.GridLines = true;
            this.lvStepDetails.HideSelection = false;
            this.lvStepDetails.Location = new Point(0, 0);
            this.lvStepDetails.Name = "lvStepDetails";
            this.lvStepDetails.ShowItemToolTips = true;
            this.lvStepDetails.Size = new Size(460, 0x7f);
            this.lvStepDetails.SmallImageList = this.ilStepIcons;
            this.lvStepDetails.TabIndex = 5;
            this.lvStepDetails.UseCompatibleStateImageBehavior = false;
            this.lvStepDetails.View = View.Details;
            this.lvStepDetails.SelectedIndexChanged += new EventHandler(this.lvStepDetails_SelectedIndexChanged);
            this.hdrStep.Text = "Step";
            this.hdrResult.Text = "Result";
            this.hdrDetails.Text = "Details";
            this.hdrDetails.Width = 0x176;
            this.scTraffic.Dock = DockStyle.Fill;
            this.scTraffic.Location = new Point(0, 0);
            this.scTraffic.Name = "scTraffic";
            this.scTraffic.Panel1.Controls.Add(this.tbRequest);
            this.scTraffic.Panel1.Controls.Add(this.lblRequest);
            this.scTraffic.Panel2.Controls.Add(this.tbResponse);
            this.scTraffic.Panel2.Controls.Add(this.lblResponse);
            this.scTraffic.Size = new Size(460, 0x105);
            this.scTraffic.SplitterDistance = 0xec;
            this.scTraffic.TabIndex = 0;
            this.tbRequest.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.tbRequest.Location = new Point(3, 0x10);
            this.tbRequest.Multiline = true;
            this.tbRequest.Name = "tbRequest";
            this.tbRequest.ReadOnly = true;
            this.tbRequest.ScrollBars = ScrollBars.Both;
            this.tbRequest.Size = new Size(230, 0xf2);
            this.tbRequest.TabIndex = 7;
            this.lblRequest.AutoSize = true;
            this.lblRequest.Location = new Point(3, 0);
            this.lblRequest.Name = "lblRequest";
            this.lblRequest.Size = new Size(50, 13);
            this.lblRequest.TabIndex = 0;
            this.lblRequest.Text = "Request:";
            this.tbResponse.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.tbResponse.Location = new Point(3, 0x11);
            this.tbResponse.Multiline = true;
            this.tbResponse.Name = "tbResponse";
            this.tbResponse.ReadOnly = true;
            this.tbResponse.ScrollBars = ScrollBars.Both;
            this.tbResponse.Size = new Size(0xd6, 0xf1);
            this.tbResponse.TabIndex = 9;
            this.lblResponse.AutoSize = true;
            this.lblResponse.Location = new Point(3, 1);
            this.lblResponse.Name = "lblResponse";
            this.lblResponse.Size = new Size(0x3a, 13);
            this.lblResponse.TabIndex = 2;
            this.lblResponse.Text = "Response:";
            this.ilStepIcons.ImageStream = (ImageListStreamer)manager.GetObject("ilStepIcons.ImageStream");
            this.ilStepIcons.TransparentColor = Color.Transparent;
            this.ilStepIcons.Images.SetKeyName(0, "NetworkOperation");
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.tcTestResults);
            base.Name = "TestResultsControl";
            base.Size = new Size(0x1da, 0x1a8);
            this.tcTestResults.ResumeLayout(false);
            this.tpTestResult.ResumeLayout(false);
            this.tpTestResult.PerformLayout();
            this.tpStepDetails.ResumeLayout(false);
            this.scStepDetails.Panel1.ResumeLayout(false);
            this.scStepDetails.Panel2.ResumeLayout(false);
            this.scStepDetails.ResumeLayout(false);
            this.scTraffic.Panel1.ResumeLayout(false);
            this.scTraffic.Panel1.PerformLayout();
            this.scTraffic.Panel2.ResumeLayout(false);
            this.scTraffic.Panel2.PerformLayout();
            this.scTraffic.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void InternalWriteLine(string logEntry)
        {
            this.tbTestResult.AppendText(string.Format("{0}{1}", logEntry, Environment.NewLine));
        }

        private void lvStepDetails_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lvStepDetails.SelectedItems.Count > 0)
            {
                ListViewItem item = this.lvStepDetails.SelectedItems[0];
                StepResult tag = (StepResult)item.Tag;
                this.tbRequest.Text = tag.Request;
                this.tbResponse.Text = tag.Response;
            }
            else
            {
                this.tbRequest.Text = string.Empty;
                this.tbResponse.Text = string.Empty;
            }
        }

        public void WriteLine(string logEntry)
        {
            base.BeginInvoke(new Action(() => this.InternalWriteLine(logEntry)));
        }
    }
}

